package com.cafuc.base;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTimeOperation {
    public void dateTimeTest(){
        Date date = new Date();
        System.out.println((date.getYear() + 1900) + "-"
                + (date.getMonth() + 1) + "-" + date.getDate());
        System.out.println(date.getHours() + "-" + date.getMinutes() + "-" + date.getSeconds());
        System.out.println(date.getTime());
        System.out.println(date.toString());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT-03:00"));
        System.out.println(simpleDateFormat.format(date));

        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println((calendar.get(Calendar.MONTH)) + 1);
        System.out.println(calendar.get(Calendar.DATE));
        System.out.println(calendar.getTimeInMillis());
        System.out.println(simpleDateFormat.format(calendar.getTime()));

        //LocalDate,LocalTime,LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.getYear());
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(localDateTime);
        dtf.format(localDateTime);

        //计算时间差
        Period period = Period.between(
                LocalDate.of(2000, 3, 14),
                LocalDate.now());
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
        System.out.println(ChronoUnit.DAYS);
        System.out.println(period.getDays());
        LocalDateTime birthday2 = LocalDateTime.of(2000, 3, 14,
                14, 30, 30);
        Duration duration = Duration.between(birthday2, localDateTime.now());
        System.out.println(duration.toDays() + "-" + duration.toHours());


    }
    public static void main(String[] args){
        DateTimeOperation dateTimeOperation = new DateTimeOperation();
        dateTimeOperation.dateTimeTest();
    }
}
